package com.chrelyonly.controllerstatic;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chrelyonly.util.myRes.AjaxJson;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chrelyonly
 * 存放controller方法
 * 目的仅仅只是整洁
 */
@Component
public class WxSeptConStatic {

    /**
     * 刷步数操作
     *
     * @return AjaxJson
     */
    public AjaxJson wxSept(String name, String pwd, String sept, String email) {
        final long l = System.currentTimeMillis();
        if (sept == null || "".equals(sept)) {
            sept = String.valueOf(RandomUtil.randomInt(20000, 80000));
        }
        String url = "https://sport.ncii.cn/api.php?callback=jQuery214012765612346224953_" + l + "&user=" + name + "&pass=" + pwd + "&step=" + sept + "&_=" + l;
        Map<String, Object> body2 = new HashMap<>(1);
        final String body = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .header("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 11; M2007J17C Build/RKQ1.200826.002)")
                .header("Connection", "Keep-Alive")
                .header("Accept-Encoding", "gzip")
                .form(body2)
                .timeout(20000)
                .execute().body();
        String str1 = body.substring(0, body.indexOf("({"));
        String str2 = body.substring(str1.length() + 1);
        String str3 = str2.substring(0, str2.length() - 1);
        final JSONObject jsonObject = JSON.parseObject(str3);
        final Object code = jsonObject.get("code");
        if (code.equals(-1)) {
            return AjaxJson.success("登陆失败： " + jsonObject);
        } else if (code.equals(1)) {
//           发送邮件
            MailAccount account = new MailAccount();
            account.setHost("smtp.qq.com");
            account.setPort(465);
            account.setAuth(true);
            account.setSslEnable(true);
            account.setFrom("1564084197@qq.com");
            account.setUser("1564084197@qq.com");
            account.setPass("nalhwhdjpezoibaa");
            MailUtil.send(account, CollUtil.newArrayList(email), "部署提交", "提交成功" + jsonObject, false);
        }
        return AjaxJson.success().put("body", body);
    }
}
